package com.zhuxiang.test5;

import java.io.*;
import java.net.Socket;
import java.util.List;

/**
 * 服务器线程类
 */
public class ServerThread extends Thread {

    private Socket s = null;
    private List<Socket> list;

    public ServerThread(Socket s, List<Socket> list) {
        this.s = s;
        this.list = list;
    }

    @Override
    public void run(){
        BufferedReader br = null;
        PrintStream ps = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        try {
            //读取客户端发来的消息
            br = new BufferedReader(new InputStreamReader((s.getInputStream())));
            ps = new PrintStream(s.getOutputStream());

            while (true){
                String str = br.readLine();
                //根据str的值判断执行什么操作
                if ("file".equalsIgnoreCase(str)){
                    sendFile(br);
                } else if ("bye".equalsIgnoreCase(str)) {
                    System.out.println(s.getPort() +"已下线！" );
                    break;
                }else {
                    for (Socket s1 : list){
                        //不发给自己
                        if (s.getPort() != s1.getPort()){
                            ps = new PrintStream(s1.getOutputStream());
                            ps.println(str);
                            System.out.println(s.getPort()+"服务器转发消息成功"+s1.getPort());
                        }
                    }
                }
                }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != ps){
                ps.close();
            }
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != s) {
                try {
                    s.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    /**
     * 上传文件到服务器，即保存文件到D://server目录下
     * @param br
     * @throws IOException
     */
    private void sendFile(BufferedReader br) throws IOException {
        String fileName = br.readLine();
        BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:/server/"+ fileName));
        byte[] bytes = new byte[1021*2];
        int len = 0;
        while ((len = bis.read(bytes)) != -1){
            bos.write(bytes,0,len);
            if (len < bytes.length){
                break;
            }
        }
        bos.flush();
        s.shutdownOutput();

        PrintStream ps = null;
        File file = new File("D://server//"+ fileName);
        BufferedInputStream bis1 = new BufferedInputStream(new FileInputStream(file));
        byte[] bytes1 = new byte[1024];
        len = 0;
        for (Socket s2 : list){
            if (s.getPort() != s2.getPort()){
                ps = new PrintStream(s2.getOutputStream());
                BufferedOutputStream bos1 = new BufferedOutputStream(s2.getOutputStream());
                ps.println("file");
                ps.println(fileName);
                while ((len = bis1.read(bytes)) != -1){
                    bos1.write(bytes,0,len);
                    bos1.flush();
                    s2.shutdownOutput();
                }
            }
        }
    }
}
